package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.WorkComment;
import com.ketangpai.admin.service.WorkCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import com.ketangpai.admin.common.Result;

@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/api")
public class WorkCommentController {
    @Autowired
    private WorkCommentService workCommentService;

    @GetMapping("/work/{workId}/comments")
    public Result<?> getComments(
            @RequestParam Integer workId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        Map<String, Object> data = workCommentService.getCommentsList(workId, page, pageSize);
        return Result.success(data);
    }

    @PostMapping("/work/{workId}/comments")
    public Result<?> submitComment(@RequestBody WorkComment comment) {
        workCommentService.addComment(comment);
        return Result.success();
    }

    @PostMapping("/work/{workId}/comments/{parentId}/reply")
    public Result<?> submitReply(
            @PathVariable Integer workId,
            @PathVariable Integer parentId,
            @RequestParam String replyTo,  // 添加回来
            @RequestBody WorkComment reply) {
        reply.setWorkId(workId);
        reply.setParentId(parentId);
        reply.setReplyTo(replyTo);
        workCommentService.addReply(parentId, replyTo, reply);  // 保持与 Service 接口一致
        return Result.success();
    }

    @GetMapping("/work/{workId}/comments/{commentId}/replies")
    public Result<?> getReplies(@PathVariable Integer workId,
                                @PathVariable Integer commentId) {
        List<WorkComment> replies = workCommentService.getReplies(commentId);
        return Result.success(replies);
    }

    @DeleteMapping("/comments/{commentId}")
    public Result<?> deleteComment(@PathVariable Integer commentId) {
        workCommentService.deleteComment(commentId);
        return Result.success();
    }

}